package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Iterator;

@RestController
@RequestMapping("/common")
public class CommonController {

    @RequestMapping("/findAllAuthority")
    public Result findAllAuthority(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();


        Collection<GrantedAuthority> authorities = user.getAuthorities();



        StringBuilder sb = new StringBuilder();


        Iterator<GrantedAuthority> iterator = authorities.iterator();
        while (iterator.hasNext()) {

            GrantedAuthority authority = iterator.next();
            sb.append(authority.getAuthority() + ",");

        }


        return new Result(true , MessageConstant.QUERY_PERMISSION_SUCCESS , sb.toString());

    }

}
